<template>
  <div style="margin: 20px 20px">
    <el-card>
      <div slot="header" class="clearfix">
        车牌号码：<el-input
          v-model="params.carNumber"
          placeholder="请输入车牌号码"
          style="width: 220px; margin: 0 10px"
          size="medium "
        />
        车主姓名：<el-input
          v-model="params.personName"
          placeholder="请输入车主姓名"
          style="width: 220px; margin: 0 10px"
          size="medium "
        />
        状态：<el-select
          v-model="params.cardStatus"
          placeholder="未选择"
          style="width: 220px"
          size="medium "
        >
          <el-option :value="0" label="可用" />
          <el-option :value="1" label="已过期" />
        </el-select>
        <el-button
          type="primary"
          size="medium"
          style="margin-left: 20px"
          @click="onInquire"
        >查询</el-button>
      </div>
      <el-row type="flex" justify="space-between" style="margin: 10px 0">
        <div>
          <el-button type="primary" size="small" @click="$router.push('/monthCard/addMonthCard')">添加月卡</el-button>
          <el-button type="primary" size="small" @click="onDelAll">批量删除</el-button>
        </div>

        <el-tag>园区共计288254个车位，月卡用户0人，车位占有率0.00%</el-tag>
      </el-row>
      <!-- 表格 -->
      <el-table
        :data="list"
        tooltip-effect="dark"
        style="width: 100%; margin: 20px 0 0 0"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" width="120">
          <template #default="{$index}">
            {{ $index+1+5*(params.page-1) }}
          </template>
        </el-table-column>
        <el-table-column label="车主名称" prop="personName" width="120" />
        <el-table-column
          prop="phoneNumber"
          label="联系方式"
        />
        <el-table-column
          prop="carNumber"
          label="车牌号码"
        />
        <el-table-column prop="carBrand" label="车辆品牌" />
        <el-table-column
          prop="totalEffectiveDate"
          label="剩余有效天数"
        />
        <el-table-column prop="cardStatus" label="状态">
          <template #default="{row}">
            {{ row.cardStatus===1?'已过期':'可用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button type="text" @click="$router.push('/monthCard/renewalCard/'+row.id)">续费</el-button>
            <el-button type="text" @click="$router.push('/monthCard/examineCard/'+row.id)">查看</el-button>
            <el-button type="text" @click="$router.push('/monthCard/redactCard/'+row.id)">编辑</el-button>
            <el-button type="text" @click="delCard(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end" style="margin:10px 0 0 0">
        <el-pagination
          :current-page="params.page"
          :page-size="params.pageSize"
          layout="total, prev, pager, next, sizes"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="changePage"
        />
      </el-row>
    </el-card>

  </div>
</template>

<script>
import { delCardAPI, getMonthCardAPI } from '@/api/monthCard'
export default {
  data() {
    return {
      // 复选框存储
      multipleSelection: [],
      list: [],
      total: 10,
      params: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        cardStatus: ''
      }
    }
  },
  created() {
    this.onload()
  },
  methods: {
    // 点击查询
    onInquire() {
      this.onload()
    },
    async onload() {
      const res = await getMonthCardAPI(this.params)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 点击删除
    async delCard(id) {
      console.log(id)
      await this.$confirm('您确定要删除吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // 发接口
      await delCardAPI(id)
      // 刷新
      this.onload()
    },
    // 点击批量删除
    async onDelAll() {
      await this.$confirm('您确定要删除吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delCardAPI(this.multipleSelection)
      this.onload()
    },
    handleSelectionChange(val) {
      // map筛选数组中id
      this.multipleSelection = val.map(item => item.id)
      console.log(this.multipleSelection)
    },
    // 监听当前页
    changePage(val) {
      this.params.page = val
      this.onload()
    },
    // 监听点击选择每页多少条数据
    handleSizeChange(val) {
      this.params.pageSize = val
      this.onload()
    }
  }
}
</script>

<style>
/* *{
  justify-content: space-between;
} */
</style>
